/*********************************************************
 * copyright xinbohit.com 版权所有 
 * 开发人员：IvanYu
 * 创建时间：2014/11/4 16:26:22
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LEARNREN.COM.TOOL;
using System.IO;

namespace LEARNREN.COM.WebSite.Admin
{
    /// <summary>
    /// 上传图片处理程序
    /// </summary>
    public class ImageUploadHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            try
            {

                HttpPostedFile file = context.Request.Files["file"];

                string[] path = UtilityTool.GetNewPicPath(file.FileName);
                string physicalPath = path[1];
                if (!Directory.Exists(Path.GetDirectoryName(physicalPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(physicalPath));
                }
                file.SaveAs(physicalPath);
                string virtualPath = path[0];
                string response = "{result:true,virtualPath:'"+virtualPath+"'}";
                context.Response.Write(response);
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                string response = "{result:false,message:'" + ex.Message + "'}";
                context.Response.Write(response);
            }

        }
        

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}